package com.example.ribbon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.config.ribbon.ServiceCaller;
import com.example.ribbon.service.SysTableRibbonService;
import com.example.util.constant.NodeUrlConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class SysTableRibbonServiceImpl implements SysTableRibbonService {

    @Autowired
    @Lazy
    ServiceCaller serviceCaller;

    @Override
    public JSONObject pushSysTableToNode(String serverName) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("serverName",serverName);
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.SYSTABLE_FLUSHSYSTABLE)
                .queryParams(params)
                .toUriString();
        System.out.println(url);
        return serviceCaller.get(serverName,url,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject pushSysTableExtToNode(String serverName,String uuid) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("uuid",uuid);
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.SYSTABLE_FLUSHSYSTABLEEXT)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(serverName,url,JSONObject.class,new JSONObject());
    }
}
